home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / COMMON / TOOLS / VB / CABINETS / MSDAO350.CAB / icontrols / MaskedEdit / MaskedEdit.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-01-08  |  10.8 KB  |  539 lines

  1. package icontrols.MaskedEdit;
  2.  
  3. import com.ms.wd.core.Component;
  4. import com.ms.wd.core.Event;
  5. import com.ms.wd.ui.Control;
  6. import com.ms.wd.ui.Edit;
  7. import com.ms.wd.ui.KeyEvent;
  8. import com.ms.wd.win32.Windows;
  9.  
  10. public class MaskedEdit extends Edit {
  11.    private static final int CUT = 0;
  12.    private static final int DELETE = 1;
  13.    private static final int BACK = 2;
  14.    private boolean allowPrompt;
  15.    private boolean autoTab;
  16.    private FormatText formatText;
  17.    private Mask mask;
  18.    private int maxLength;
  19.    private boolean promptIncluded;
  20.    private ValidationErrorEventHandler m_validationErrorHandler;
  21.    private transient boolean canUndo;
  22.    private transient boolean focus;
  23.    private transient char lastChar;
  24.    private transient int lastCode;
  25.    private transient Object tag;
  26.    private transient int undoEnd;
  27.    private transient int undoStart;
  28.    private transient String undoText;
  29.  
  30.    public void setText(String text) {
  31.       if (!this.focus && this.formatText.getFormatString().length() != 0) {
  32.          try {
  33.             super.setText(this.formatText.reformatText(text));
  34.          } catch (IllegalArgumentException var3) {
  35.             super.setText("");
  36.             this.onValidationError(new ValidationErrorEvent(5, -1));
  37.          }
  38.       } else {
  39.          super.setText(text);
  40.       }
  41.  
  42.       this.canUndo = false;
  43.    }
  44.  
  45.    public char getPasswordChar() {
  46.       return (char)((Control)this).sendMessage(210, 0, 0);
  47.    }
  48.  
  49.    public void setPasswordChar(char passwordChar) {
  50.       ((Control)this).sendMessage(204, passwordChar, 0);
  51.    }
  52.  
  53.    public void setAutoTab(boolean autoTab) {
  54.       this.autoTab = autoTab;
  55.    }
  56.  
  57.    private void undo() {
  58.       if (this.canUndo) {
  59.          int start = ((Edit)this).getSelectionStart();
  60.          int end = ((Edit)this).getSelectionEnd();
  61.          String tmp = this.getText();
  62.          super.setText(this.undoText);
  63.          ((Edit)this).select(this.undoStart, this.undoEnd);
  64.          this.undoText = tmp;
  65.          this.undoStart = start;
  66.          this.undoEnd = end;
  67.       }
  68.    }
  69.  
  70.    public int getMaxLength() {
  71.       return this.maxLength;
  72.    }
  73.  
  74.    public String getFormat() {
  75.       return this.formatText.getFormatString();
  76.    }
  77.  
  78.    private boolean maskTextInsert(String newText, boolean ignoreError) {
  79.       int start = ((Edit)this).getSelectionStart();
  80.       if (start >= this.mask.length()) {
  81.          if (!ignoreError) {
  82.             this.onValidationError(new ValidationErrorEvent(3, -1));
  83.             Windows.MessageBeep(0);
  84.          }
  85.  
  86.          return false;
  87.       } else {
  88.          char[] text = this.getText().toCharArray();
  89.  
  90.          for(int i = 0; i < newText.length(); ++i) {
  91.             start = this.mask.skipLiteral(start, 2);
  92.             if (start == text.length) {
  93.                break;
  94.             }
  95.  
  96.             int dstChar = this.mask.skipLiteral(text.length - 1, 1);
  97.             if (!this.mask.isequalPromptChar(text[dstChar])) {
  98.                if (!ignoreError) {
  99.                   this.onValidationError(new ValidationErrorEvent(1, -1));
  100.                   Windows.MessageBeep(0);
  101.                }
  102.  
  103.                return false;
  104.             }
  105.  
  106.             if (!this.mask.isValidCharacter(start, newText.charAt(i), this.isAllowPrompt())) {
  107.                if (!ignoreError) {
  108.                   this.onValidationError(new ValidationErrorEvent(2, start));
  109.                   Windows.MessageBeep(0);
  110.                }
  111.  
  112.                return false;
  113.             }
  114.  
  115.             while(true) {
  116.                dstChar = this.mask.skipLiteral(dstChar, 1);
  117.                if (dstChar <= start) {
  118.                   text[start] = this.mask.checkCase(start, newText.charAt(i));
  119.                   ++start;
  120.                   break;
  121.                }
  122.  
  123.                int srcChar = this.mask.skipLiteral(dstChar - 1, 1);
  124.                if (!this.mask.isValidCharacter(dstChar, text[srcChar], true)) {
  125.                   if (!ignoreError) {
  126.                      this.onValidationError(new ValidationErrorEvent(2, dstChar));
  127.                      Windows.MessageBeep(0);
  128.                   }
  129.  
  130.                   return false;
  131.                }
  132.  
  133.                text[dstChar] = this.mask.checkCase(dstChar, text[srcChar]);
  134.                --dstChar;
  135.             }
  136.          }
  137.  
  138.          super.setText(new String(text));
  139.          start = this.mask.skipLiteral(start, 2);
  140.          ((Edit)this).select(start, start);
  141.          return start == this.mask.length();
  142.       }
  143.    }
  144.  
  145.    private void paste() {
  146.    }
  147.  
  148.    public void setFormat(String format, int formatType) {
  149.       this.formatText.setFormat(format, formatType);
  150.       this.canUndo = false;
  151.       if (!this.focus) {
  152.          this.setRawText(this.getRawText());
  153.       }
  154.  
  155.    }
  156.  
  157.    public void setMaxLength(int maxLength) {
  158.       this.maxLength = maxLength;
  159.       this.canUndo = false;
  160.    }
  161.  
  162.    private void maskTextCut(int state) {
  163.       boolean modified = false;
  164.       int start = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 2);
  165.       int end = this.mask.skipLiteral(((Edit)this).getSelectionEnd(), 2);
  166.       if (state == 0) {
  167.          if (start == end) {
  168.             return;
  169.          }
  170.       } else if (start == end) {
  171.          if (state == 1) {
  172.             start = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 2);
  173.             if (start == this.mask.length()) {
  174.                return;
  175.             }
  176.  
  177.             end = this.mask.skipLiteral(start + 1, 2);
  178.          } else {
  179.             if (state != 2) {
  180.                throw new Error("maskTextCut: Internal Error");
  181.             }
  182.  
  183.             end = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 1);
  184.             if (end == 0) {
  185.                return;
  186.             }
  187.  
  188.             start = this.mask.skipLiteral(end - 1, 1);
  189.          }
  190.       }
  191.  
  192.       char[] text = this.getText().toCharArray();
  193.       int srcChar = 0;
  194.       int dstChar = 0;
  195.  
  196.       while(true) {
  197.          srcChar = this.mask.skipLiteral(srcChar, 2);
  198.          dstChar = this.mask.skipLiteral(dstChar, 2);
  199.          if (srcChar == text.length) {
  200.             break;
  201.          }
  202.  
  203.          if (srcChar < start || srcChar >= end) {
  204.             if (!this.mask.isValidCharacter(dstChar, text[srcChar], true)) {
  205.                break;
  206.             }
  207.  
  208.             text[dstChar] = this.mask.checkCase(dstChar, text[srcChar]);
  209.             ++dstChar;
  210.             if (state == 0 || state == 2 && this.lastChar != '\b' || state == 1 && this.lastCode != 46) {
  211.                modified = true;
  212.             }
  213.          }
  214.  
  215.          ++srcChar;
  216.       }
  217.  
  218.       for(; dstChar < srcChar; ++dstChar) {
  219.          if (this.mask.isMetaChar(dstChar)) {
  220.             text[dstChar] = this.getPromptChar();
  221.          }
  222.       }
  223.  
  224.       if (modified) {
  225.          this.undoSave();
  226.       }
  227.  
  228.       super.setText(new String(text));
  229.       start = this.mask.skipLiteral(start, 2);
  230.       ((Edit)this).select(start, start);
  231.    }
  232.  
  233.    private void cut() {
  234.       this.copy();
  235.       this.maskTextCut(0);
  236.    }
  237.  
  238.    public boolean isPromptIncluded() {
  239.       return this.promptIncluded;
  240.    }
  241.  
  242.    private void undoSave() {
  243.       this.undoStart = ((Edit)this).getSelectionStart();
  244.       this.undoEnd = ((Edit)this).getSelectionEnd();
  245.       this.undoText = this.getText();
  246.       this.canUndo = true;
  247.    }
  248.  
  249.    protected void onKeyDown(KeyEvent e) {
  250.       if (((Component)this).isDesignMode()) {
  251.          e.handled = true;
  252.       } else {
  253.          if (this.mask.isMaskSet()) {
  254.             switch (e.getKeyCode()) {
  255.                case 45:
  256.                   if (e.isControl()) {
  257.                      this.copy();
  258.                   } else if (e.isShift()) {
  259.                      this.paste();
  260.                   }
  261.  
  262.                   this.lastCode = e.getKeyCode();
  263.                   e.handled = true;
  264.                   return;
  265.                case 46:
  266.                   if (e.isShift()) {
  267.                      this.cut();
  268.                   } else {
  269.                      this.maskTextCut(1);
  270.                   }
  271.  
  272.                   this.lastCode = e.getKeyCode();
  273.                   e.handled = true;
  274.                   return;
  275.             }
  276.          }
  277.  
  278.          this.lastCode = e.getKeyCode();
  279.          super.onKeyDown(e);
  280.       }
  281.    }
  282.  
  283.    protected void onLostFocus(Event e) {
  284.       if (!((Component)this).isDesignMode()) {
  285.          this.canUndo = false;
  286.          if (this.mask.isMaskSet()) {
  287.             String s = this.getText();
  288.  
  289.             for(int i = 0; i < s.length(); ++i) {
  290.                if (!this.mask.isValidCharacter(i, s.charAt(i), this.isAllowPrompt())) {
  291.                   this.onValidationError(new ValidationErrorEvent(4, i));
  292.                   break;
  293.                }
  294.             }
  295.          }
  296.  
  297.          if (this.formatText.getFormatString().length() != 0 && this.getRawText().length() != 0) {
  298.             this.undoSave();
  299.  
  300.             try {
  301.                this.focus = false;
  302.                super.setText(this.formatText.reformatText(this.getText()));
  303.             } catch (IllegalArgumentException var4) {
  304.                this.canUndo = false;
  305.                this.onValidationError(new ValidationErrorEvent(5, -1));
  306.             }
  307.          }
  308.  
  309.          this.focus = false;
  310.       }
  311.  
  312.       super.onLostFocus(e);
  313.    }
  314.  
  315.    public boolean isEnabled() {
  316.       return Windows.IsWindowEnabled(((Control)this).getHandle());
  317.    }
  318.  
  319.    public String toString() {
  320.       return "{allowPrompt=" + this.allowPrompt + ",autoTab=" + this.autoTab + ",formatText=" + this.formatText.toString() + ",mask=" + this.mask.toString() + ",maxLength=" + this.maxLength + ",promptIncluded=" + this.promptIncluded + "}";
  321.    }
  322.  
  323.    public MaskedEdit() {
  324.       this("");
  325.    }
  326.  
  327.    public MaskedEdit(String text) {
  328.       this.allowPrompt = false;
  329.       this.autoTab = false;
  330.       this.formatText = new FormatText();
  331.       this.mask = new Mask();
  332.       this.maxLength = 0;
  333.       this.promptIncluded = false;
  334.       this.canUndo = false;
  335.       this.focus = false;
  336.       this.lastChar = 0;
  337.       this.lastCode = 0;
  338.       this.tag = null;
  339.       this.undoEnd = 0;
  340.       this.undoStart = 0;
  341.       this.undoText = new String("");
  342.       this.setText(text);
  343.    }
  344.  
  345.    public boolean isAutoTab() {
  346.       return this.autoTab;
  347.    }
  348.  
  349.    public Object getTag() {
  350.       return this.tag;
  351.    }
  352.  
  353.    public void setTag(Object tag) {
  354.       this.tag = tag;
  355.    }
  356.  
  357.    public boolean isAllowPrompt() {
  358.       return this.allowPrompt;
  359.    }
  360.  
  361.    public char getPromptChar() {
  362.       return this.mask.getPromptChar();
  363.    }
  364.  
  365.    public void setPromptChar(char promptChar) {
  366.       this.mask.setPromptChar(promptChar);
  367.       this.canUndo = false;
  368.    }
  369.  
  370.    public void setPromptIncluded(boolean promptIncluded) {
  371.       this.promptIncluded = promptIncluded;
  372.       this.canUndo = false;
  373.    }
  374.  
  375.    private void copy() {
  376.       int start = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 2);
  377.       int end = this.mask.skipLiteral(((Edit)this).getSelectionEnd(), 2);
  378.       if (start != end) {
  379.          ;
  380.       }
  381.    }
  382.  
  383.    protected void onKeyPress(KeyEvent e) {
  384.       if (((Component)this).isDesignMode()) {
  385.          e.handled = true;
  386.       } else if (this.mask.isMaskSet()) {
  387.          switch (e.keyChar) {
  388.             case '\u0003':
  389.                this.copy();
  390.                break;
  391.             case '\b':
  392.                if ((Control.getModifierKeys() & 262144) != 0) {
  393.                   this.undo();
  394.                   e.handled = true;
  395.                   return;
  396.                }
  397.  
  398.                this.maskTextCut(2);
  399.                break;
  400.             case '\u0016':
  401.                this.paste();
  402.                break;
  403.             case '\u0018':
  404.                this.cut();
  405.                break;
  406.             default:
  407.                this.maskTextCut(0);
  408.                if (this.maskTextInsert((new Character(e.keyChar)).toString(), false) && this.isAutoTab()) {
  409.                   ((Control)this).getParent().selectNextControl(this, true, true, true, false);
  410.                }
  411.          }
  412.  
  413.          this.lastChar = e.keyChar;
  414.          e.handled = true;
  415.       } else {
  416.          if (this.getMaxLength() > 0) {
  417.             if (e.keyChar != '\b' && this.getText().length() == this.getMaxLength()) {
  418.                Windows.MessageBeep(0);
  419.                e.handled = true;
  420.                return;
  421.             }
  422.  
  423.             if (this.isAutoTab() && ((Edit)this).getSelectionStart() == this.getMaxLength() - 1) {
  424.                ((Control)this).getParent().selectNextControl(this, true, true, true, false);
  425.             }
  426.          }
  427.  
  428.          this.lastChar = e.keyChar;
  429.          super.onKeyPress(e);
  430.       }
  431.    }
  432.  
  433.    public String getRawText() {
  434.       if (!this.mask.isMaskSet()) {
  435.          return this.getText();
  436.       } else {
  437.          String result = "";
  438.          String text = this.getText();
  439.          int enterable = 0;
  440.  
  441.          for(int i = 0; i < this.mask.length() && i < text.length(); ++i) {
  442.             if (this.mask.isMetaChar(i)) {
  443.                ++enterable;
  444.                if (this.promptIncluded || !this.mask.isequalPromptChar(text.charAt(i))) {
  445.                   result = result + text.substring(i, i + 1);
  446.                }
  447.             }
  448.          }
  449.  
  450.          if (this.promptIncluded) {
  451.             while(result.length() < enterable) {
  452.                result = result + this.mask.getPromptChar();
  453.             }
  454.          }
  455.  
  456.          return result;
  457.       }
  458.    }
  459.  
  460.    public void setRawText(String rawText) {
  461.       if (!this.focus && this.formatText.getFormatString().length() != 0) {
  462.          try {
  463.             super.setText(this.formatText.reformatText(rawText));
  464.          } catch (IllegalArgumentException var3) {
  465.             super.setText("");
  466.             this.onValidationError(new ValidationErrorEvent(5, -1));
  467.          }
  468.       } else if (!this.mask.isMaskSet()) {
  469.          super.setText(rawText);
  470.       } else {
  471.          super.setText(this.mask.getEmptyMask());
  472.  
  473.          for(int i = 0; i < rawText.length() && !this.maskTextInsert(rawText.substring(i, i + 1), true); ++i) {
  474.          }
  475.       }
  476.  
  477.       this.canUndo = false;
  478.    }
  479.  
  480.    protected void onGotFocus(Event e) {
  481.       if (!((Component)this).isDesignMode()) {
  482.          this.focus = true;
  483.          if (this.formatText.getFormatString().length() != 0) {
  484.             this.undo();
  485.          }
  486.       }
  487.  
  488.       super.onGotFocus(e);
  489.    }
  490.  
  491.    public String getMask() {
  492.       return this.mask.getMask();
  493.    }
  494.  
  495.    public void setMask(String mask) {
  496.       this.mask = new Mask(mask, this.mask.getPromptChar());
  497.       this.setMaxLength(this.mask.length());
  498.       this.canUndo = false;
  499.       if (((Component)this).isDesignMode()) {
  500.          super.setText(this.mask.getEmptyMask());
  501.       } else {
  502.          this.setRawText("");
  503.       }
  504.  
  505.    }
  506.  
  507.    public void setEnabled(boolean enabled) {
  508.       Windows.EnableWindow(((Control)this).getHandle(), enabled);
  509.    }
  510.  
  511.    public ValidationErrorEventHandler getOnValidationError() {
  512.       return this.m_validationErrorHandler;
  513.    }
  514.  
  515.    protected void onValidationError(ValidationErrorEvent event) {
  516.       if (this.m_validationErrorHandler != null) {
  517.          this.m_validationErrorHandler.invoke(this, event);
  518.       }
  519.  
  520.    }
  521.  
  522.    public void setOnValidationError(ValidationErrorEventHandler handler) {
  523.       this.m_validationErrorHandler = handler;
  524.    }
  525.  
  526.    public String getText() {
  527.       if (!((Component)this).isDesignMode() && this.formatText.getFormatString().length() != 0 && !this.focus) {
  528.          return this.canUndo ? this.undoText : "";
  529.       } else {
  530.          return super.getText();
  531.       }
  532.    }
  533.  
  534.    public void setAllowPrompt(boolean allowPrompt) {
  535.       this.allowPrompt = allowPrompt;
  536.       this.canUndo = false;
  537.    }
  538. }
  539.